//
//  JCComposePhotoView.swift
//  SinaWeibo_18
//
//  Created by apple on 15/10/27.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCComposePhotoView: UIImageView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(deleteButton)
        //开启用户交互
        userInteractionEnabled = true
        //
        contentMode = UIViewContentMode.ScaleAspectFill
        clipsToBounds = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        deleteButton.x = width - deleteButton.width
    }
    //删除按钮点击  图片动画效果
    @objc private func deleteButtonClick(){
        
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.alpha = 0.0
        
            }) { (finish) -> Void in
                //移除
                self.removeFromSuperview()
        }
    }
    
    //删除按钮控件的初始化
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        //设置大小
        button.sizeToFit()
        return button
    }()
    
}
